# -*- coding: utf-8 -*-

### 聊天相关的数据模型和Schema
# backend/schemas/chat.py
from pydantic import BaseModel, Field
from typing import List, Optional, Dict, Any
from datetime import datetime


class ChatMessageRequest(BaseModel):
    """聊天消息请求"""
    message: str = Field(..., min_length=1, max_length=10000, description="用户消息")
    project_id: Optional[str] = Field(None, description="项目ID")
    history: Optional[List[Dict[str, Any]]] = Field(default=[], description="对话历史")
    context: Optional[Dict[str, Any]] = Field(default={}, description="上下文信息")
    
    class Config:
        schema_extra = {
            "example": {
                "message": "帮我写一个Python函数来读取CSV文件",
                "project_id": "proj_123",
                "history": [
                    {"role": "user", "content": "你好"},
                    {"role": "assistant", "content": "你好！我是你的编程助手，有什么可以帮助你的吗？"}
                ],
                "context": {}
            }
        }


class StreamingChatRequest(BaseModel):
    """流式聊天请求"""
    message: str = Field(..., min_length=1, max_length=10000)
    project_id: Optional[str] = None
    history: Optional[List[Dict[str, Any]]] = []
    
    class Config:
        schema_extra = {
            "example": {
                "message": "解释一下这段代码的作用 @main-v0.0.py",
                "project_id": "proj_123",
                "history": []
            }
        }


class CodeBlock(BaseModel):
    """代码块"""
    language: str = Field(..., description="编程语言")
    code: str = Field(..., description="代码内容")
    filename: Optional[str] = Field(None, description="建议的文件名")
    description: Optional[str] = Field(None, description="代码描述")


class ChatMessageResponse(BaseModel):
    """聊天消息响应"""
    message: str = Field(..., description="AI回复消息")
    type: str = Field(default="text", description="消息类型")
    metadata: Dict[str, Any] = Field(default={}, description="元数据")
    suggestions: List[str] = Field(default=[], description="建议列表")
    code_blocks: List[CodeBlock] = Field(default=[], description="代码块列表")
    timestamp: datetime = Field(default_factory=datetime.utcnow, description="时间戳")
    
    class Config:
        schema_extra = {
            "example": {
                "message": "这是一个读取CSV文件的Python函数...",
                "type": "code_generation",
                "metadata": {"language": "python", "complexity": "medium"},
                "suggestions": ["添加错误处理", "考虑大文件处理"],
                "code_blocks": [
                    {
                        "language": "python",
                        "code": "import pandas as pd\n\ndef read_csv_file(filepath):\n    return pd.read_csv(filepath)",
                        "filename": "csv_reader.py",
                        "description": "CSV文件读取函数"
                    }
                ],
                "timestamp": "2024-01-01T12:00:00Z"
            }
        }


class ChatHistoryResponse(BaseModel):
    """聊天历史响应"""
    messages: List[Dict[str, Any]] = Field(..., description="消息列表")
    total: int = Field(..., description="总消息数")
    page: int = Field(..., description="当前页码")
    size: int = Field(..., description="每页大小")
    has_more: bool = Field(..., description="是否有更多消息")


class ChatSessionResponse(BaseModel):
    """聊天会话响应"""
    project_id: str = Field(..., description="项目ID")
    project_name: str = Field(..., description="项目名称")
    last_message: str = Field(..., description="最后一条消息")
    last_message_time: datetime = Field(..., description="最后消息时间")
    message_count: int = Field(..., description="消息总数")
